import {
  Inject,
  Controller,
  Provide,
  Body,
  ALL,
  All,
  Validate,
  Config,
  Plugin,
} from '@midwayjs/decorator';
import { Context } from 'egg';
import { UserDTO } from '../../dto/user';
// import { User } from '../../entity/user';
// import { IUserOptions } from '../../interface';
import { UserService } from '../../service/user';
import { BaseController } from '../base';

@Provide()
@Controller('/admin')
export class LoginController extends BaseController {
  @Config('jwt')
  jwtConfig;
  @Plugin()
  jwt: any;
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @All('/login')
  @Validate()
  async login(@Body(ALL) params: UserDTO) {
    const user = await this.userService.getUserInfoByLogin(params);

    const token = this.jwt.sign(
      { username: user.username },
      this.jwtConfig.secret
    );
    return this.successResponse({
      token,
      name: user.username,
    });
  }
}
